/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.univcasa.biz.impl;

import com.univcasa.biz.MoodleManager;
import com.univcasa.biz.SyncComptesENTLdap;
import com.univcasa.db.model.CompteEtudiant;
import com.univcasa.db.model.ComptePersonnel;
import com.univcasa.db.model.Criteres;
import com.univcasa.managing.ApogeeStudentsManager;
import com.univcasa.managing.ComptesPersManager;
import com.univcasa.managing.ComptesStudentsManager;
import com.univcasa.managing.LdapDBManager;
import com.univcasa.managing.impl.ApogeeStudentsManagerImpl;
import com.univcasa.managing.impl.ComptesPersManagerImpl;
import com.univcasa.managing.impl.ComptesStudentsManagerImpl;
import com.univcasa.managing.impl.LdapDBManagerImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author issam
 */
public class SyncComptesENTLdapImpl implements SyncComptesENTLdap{

    private ComptesStudentsManager comptesManager;
    private ComptesPersManager comptesPersManger;
    private ApogeeStudentsManager apogeeManager;
    private LdapDBManager ldapManager;
    private MoodleManager moodleManager;


    public SyncComptesENTLdapImpl() {
        comptesManager = new ComptesStudentsManagerImpl();
        comptesPersManger = new ComptesPersManagerImpl();
        apogeeManager = new ApogeeStudentsManagerImpl();;
        ldapManager = new LdapDBManagerImpl();
        moodleManager = new MoodleManagerImpl();
    }

    public List ajouterComptesEntStudentssurLdapByApo(List apogees){

        List listapogeeComptesLdap = ldapManager.recupererListeApogeeEtudiants();
        List successAdded = new ArrayList();
        for (Iterator itr = apogees.iterator() ; itr.hasNext();){
            String apogee = (String) itr.next() ;
            //verifier si l'apogee existe
            if( listapogeeComptesLdap==null || ! listapogeeComptesLdap.contains( apogee ) ){
                //ajout du compte sur Ldao
                CompteEtudiant etudiant = comptesManager.getCompteEtudiantByApogee(apogee);
                boolean ajout = ldapManager.ajoutCompteEtudiantLdap(etudiant);
                if ( ajout == true ){
                    comptesManager.majCompteEtudiant(etudiant);
                    moodleManager.addStds2Moodle(etudiant);
                    successAdded.add( etudiant );
                }
            }
        }
            return successAdded ;
    }

    public List ajouterComptesEntStudentssurLdap(Criteres criteres){

        List listapogeeComptes = comptesManager.getListComptesStudentsByCriteria(criteres);
        List listapogeeComptesLdap = ldapManager.recupererListeApogeeEtudiants();
        List successAdded = new ArrayList();
        for (Iterator itr = listapogeeComptes.iterator() ; itr.hasNext();){
            CompteEtudiant std = (CompteEtudiant) itr.next();
            String apogee = std.getApogee() ;
            //verifier si l'apogee existe
            if( listapogeeComptesLdap==null || ! listapogeeComptesLdap.contains( apogee ) ){
                //ajout du compte sur Ldao
                CompteEtudiant etudiant = comptesManager.getCompteEtudiantByApogee(apogee);
                boolean ajout = ldapManager.ajoutCompteEtudiantLdap(etudiant);
                if ( ajout == true ){
                    comptesManager.majCompteEtudiant(etudiant);
                    moodleManager.addStds2Moodle(etudiant);
                    successAdded.add( etudiant );
                }                
            }
        }        
            return successAdded ;
    }

    public List initStudentsLdapSurComptesENT() {
        /**
        List listApogeeStdsLdap = ldapManager.recupererListeApogeeEtudiants();
        List listApogeeStdsDBComptes = comptesManager.getAllApogeeComptesStudents() ;

        
         * @todo traiter le souci des etabs . length > 3 chars diploma.length() <= 7
         
        for (Iterator itr = listApogeeStdsLdap.iterator() ; itr.hasNext();){
            String apogee = (String) itr.next();
            
            if( ! listApogeeStdsDBComptes.contains( apogee ) ){
                //recuprer le compte Etudiant depuis ldap et l'ajouter sur DBComptes
                CompteEtudiant etudiant = ldapManager.recupererEtudiantByApogee(apogee);
                String etape = etudiant.getEtape();
                String etab = etudiant.getEtab();
                String diplome = etudiant.getDiplome();
                if (etape == null ||etape.length() > 6 || etab==null|| etab.length() > 3 || diplome == null ||  diplome.length() > 7 ){
                    HashMap form = new HashMap();
                    form.put("annee", "%" ) ;
                    form.put("etab", "%");
                    form.put("apogee", apogee );
                    List stds = apogeeManager.getStdsFromApogeeByCriteria( form ,1 );
                    CompteEtudiant etudiant2 = new CompteEtudiant();
                    for (Iterator itr2 = stds.iterator() ; itr2.hasNext();){
                        etudiant2 = (CompteEtudiant) itr2.next();
                        break; }
                    String etape2 = etudiant2.getEtape();
                    String etab2 = etudiant2.getEtab();
                    String diplome2 = etudiant2.getDiplome();
                    etudiant.setEtab(etab2);
                    etudiant.setEtape(etape2);
                    etudiant.setDiplome(diplome2);
                }
                comptesManager.ajouterCompteEtudiant(etudiant);
            }
        }*/
        return null;
    }


    public List initPersLdapSurComptesENT() {

        List listSomPersLdap = ldapManager.recupererListesomPers();
        List listSomPersDBComptes = comptesPersManger.getAllSomComptesPers() ;

        /**
         * @todo traiter le souci des etabs . length > 3 chars diploma.length() <= 7
         */
        for (Iterator itr = listSomPersLdap.iterator() ; itr.hasNext();){
            String som = (String) itr.next();

            if( ! listSomPersDBComptes.contains( som ) ){
                //recuprer le compte Etudiant depuis ldap et l'ajouter sur DBComptes
                ComptePersonnel pers = ldapManager.recupererPersBySom(som);
                System.out.println("PERS " + pers);
                if(pers.getPassword().contains("{SHA}") || pers.getPassword().contains("{MD5}") ){
                    pers.setPassword( pers.getSom() );
                }
                comptesPersManger.ajouterComptePers(pers);
            }
        }
        return null;        
    }

    public List searchComptesStd2AddSurLdap(Criteres criteres){
        List listApogeeStdsLdap = ldapManager.recupererListeApogeeEtudiants();
        List listApogeeStdsDBComptes = comptesManager.getApogeeComptesStudents(criteres) ;
        List listStds2AddonLdap = new ArrayList();
        
        for (Iterator itr = listApogeeStdsDBComptes.iterator() ; itr.hasNext();){
            String apogee = (String) itr.next();;
            if ( !listApogeeStdsLdap.contains(apogee)  ){
                listStds2AddonLdap.add( comptesManager.getCompteEtudiantByApogee(apogee) );
            }
        }
        return listStds2AddonLdap;
    }

    public List searchComptesPers2AddSurLdap(Criteres criteres){

        List listSomPersLdap = ldapManager.recupererListesomPers();
        List listSomPersDBComptes = comptesPersManger.getAllSomComptesPers();
        List listPers2AddonLdap = new ArrayList();
        Criteres crit = new Criteres();

        for (Iterator itr = listSomPersDBComptes.iterator() ; itr.hasNext();){
            String som = (String) itr.next();
            if ( !listSomPersLdap.contains(som)  ){
                crit.addCritere("som", som);
                listPers2AddonLdap.add( comptesPersManger.getComptePersByCriteria(crit));
            }
        }
        return listPers2AddonLdap;
    }

    public List ajouterComptesEntPersssurLdap(Criteres criteres) {
        List listsomComptes = comptesPersManger.getAllSomComptesPers();
        List listsomComptesLdap = ldapManager.recupererListesomPers();
        List successAdded = new ArrayList();
        for (Iterator itr = listsomComptes.iterator() ; itr.hasNext();){            
            String som = (String) itr.next();
            //verifier si l'apogee existe
            if( listsomComptesLdap==null || ! listsomComptesLdap.contains( som ) ){
                //ajout du compte sur Ldao
                Criteres crits = new Criteres();
                crits.put("som",som);
                ComptePersonnel pers = comptesPersManger.getComptePersByCriteria(crits);
                boolean ajout = ldapManager.ajoutComptePersLdap(pers);
                if ( ajout == true ){
                    comptesPersManger.majComptePers(pers);
                    moodleManager.addPers2Moodle(pers);
                    successAdded.add( pers );
                }
            }
        }
            return successAdded ;
    }
}